package com.boyuan.system.exception;

import com.boyuan.system.common.BaseResponse;
import com.boyuan.system.common.R;
import com.boyuan.system.model.vo.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLSyntaxErrorException;

/**
 * 全局错误处理
 * @author ply
 * @date 2023-11-20 15:16
 */
@RestControllerAdvice
public class ExceptionAdvice {
    private static final Logger log = LoggerFactory.getLogger(ExceptionAdvice.class);

    /**
     * 传递参数异常
     *
     * @param e 错误异常
     * @return 结果
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public AjaxResult exceptionHandlerParamsException(IllegalArgumentException e) {
        log.error("传递参数异常：{}", e.getMessage());
        if (StringUtils.isEmpty(e.getMessage())) {
            return AjaxResult.error("操作失败");
        }
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 请求方法错误
     *
     * @return 结果
     */
    @ResponseBody
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public AjaxResult httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException httpRequestMethodNotSupportedException) {
        log.error("请求方法错误：{}", httpRequestMethodNotSupportedException.getMessage());
        AjaxResult ajax = AjaxResult.error();
        ajax.put("errMsg", httpRequestMethodNotSupportedException.getMessage());
        return ajax;
    }

    /**
     * 登录错误异常
     *
     * @param e 错误异常
     * @return 结果
     */
    @ExceptionHandler(LoginException.class)
    public AjaxResult handleLoginException(LoginException e) {
        log.error("登录错误信息：{}", e.getMessage());
        return AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
    }

    /**
     * SQL语句错误信息
     *
     * @param e 错误异常
     * @return 结果
     */
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public AjaxResult handleSQLSyntaxErrorException(SQLSyntaxErrorException e) {
        log.error("SQL语句错误信息：{}", e.getMessage());
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址：{},发生未知异常：{}", requestURI, e.getMessage());
        if (e.getMessage().contains("SQLIntegrityConstraintViolationException")) {
            return AjaxResult.error("当前页面数据字段违反数据库唯一索引，请检查页面信息");
        } else if (e.getMessage().contains("MysqlDataTruncation: Data truncation: Data too long")) {
            return AjaxResult.error("字段数据过长，请检查字段信息");
        }
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult handleException(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return AjaxResult.error(e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException", e);
        return R.error(e.getCode(), e.getMessage());
    }
}
